﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution2
{
    //l1和l2不一样长
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2)//简化一下
    {
        ListNode head = new ListNode();
        ListNode cur= head;
        int next = 0;
        int curNum = 0;
        while (l1 != null||l2!=null)
        {

            int num1=l1!=null?l1.val:0;
            int num2=l2 != null ? l2.val:0;
            int sum= num1+num2 + next;
            next = sum / 10;
             curNum=sum % 10;

            ListNode newA = new ListNode(curNum);
            cur.next=newA;
            cur=newA;
            if (l1 != null)
            {
                l1 = l1.next;

            }
              if  (l2 != null)
            {
                l2 = l2.next;
            }
           




        }

      

        if (next != 0)
        {
            ListNode newA = new ListNode(next);
            cur.next = newA;
            cur = newA;
        }

        return head.next;



    }
    public ListNode AddTwoNumbers2(ListNode l1, ListNode l2)//简化一下
    {
        ListNode head = new ListNode();
        ListNode cur = head;
        int next = 0;
        int curNum = 0;
        while (l1 != null && l2 != null)
        {
            int sum = l1.val + l2.val + next;
            next = sum / 10;
            curNum = sum % 10;

            ListNode newA = new ListNode(curNum);
            cur.next = newA;
            cur = newA;
            l1 = l1.next;
            l2 = l2.next;

        }

        while (l1 != null)
        {
            int sum = l1.val + next;
            next = sum / 10;
            curNum = sum % 10;

            ListNode newA = new ListNode(curNum);
            cur.next = newA;
            cur = newA;
            l1 = l1.next;

        }
        while (l2 != null)
        {
            int sum = l2.val + next;
            next = sum / 10;
            curNum = sum % 10;

            ListNode newA = new ListNode(curNum);
            cur.next = newA;
            cur = newA;
            l2 = l2.next;

        }

        if (next != 0)
        {
            ListNode newA = new ListNode(next);
            cur.next = newA;
            cur = newA;
        }

        return head.next;



    }
}
class Program2
{

    static void Main2(string[] args)
    {

        Solution2 solution = new Solution2();
        //int[] l1 = [2, 4, 3], l2 = [5, 6, 4];
        //int[] l1 = [0], l2 = [0];
        int[] l1 = [9, 9, 9, 9, 9, 9, 9], l2 = [9, 9, 9, 9];
        ListNode headA=new ListNode();
        ListNode curA= headA;
        for (int i = 0; i < l1.Length; i++)
        {
            ListNode newA = new ListNode(l1[i]);
            curA.next = newA;
            curA = newA;

        }
        ListNode headB = new ListNode();
        ListNode curB = headB;
        for (int i = 0; i < l2.Length; i++)
        {
            ListNode newB = new ListNode(l2[i]);
            curB.next = newB;
            curB = newB;

        }

        ListNode res= solution.AddTwoNumbers(headA.next, headB.next);
        while (res != null) {



            Console.WriteLine(res.val);
            res=res.next;
        }

    }
}
